package sky.wicket;

import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import sky.domain.City;
import sky.service.CityService;
import sky.tro.CityFinder;
import sky.wicket.panels.CityPanel;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 06.10.13
 * Time: 17:35
 * To change this template use File | Settings | File Templates.
 */
public class CityList extends BasePage {

    @SpringBean
    CityService cityService;
    List<City> cityList;

    public CityList() {
        cityList = cityService.listCity();
        addElementsToPage();
    }

    public CityList(CityFinder cityFinder) {
        cityList = cityService.listCityByFinder(cityFinder);
        addElementsToPage();
    }

    private void addElementsToPage() {
        //add filter link
        MyWicketUtils.addBookmarkablePageLinkWithLabel(this, "cityFilterLink", CityFilter.class,
                "cityFilterLabel", "Filter");
        //add airport link
        MyWicketUtils.addBookmarkablePageLinkWithLabel(this, "addCityLink", AddUpdateCity.class,
                "addCityLabel", "Add City");
        //list of flights
        ListView<City> cityListView = new ListView<City>("cityList", cityList) {
            @Override
            protected void populateItem(ListItem<City> component) {
                final City city = component.getModelObject();
                addComponentsToList(component, city);
            }
        };
        add(cityListView);
    }

    private void addComponentsToList(ListItem<City> component, final City city) {
        component.add(new CityPanel("cityPanel", new Model<City>(city), true));
    }
}
